package com.ruoyi.course.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.SysConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.course.domain.SysCourseTopic;
import com.ruoyi.course.domain.SysPaper;
import com.ruoyi.course.domain.SysPaperTopic;
import com.ruoyi.course.mapper.SysPaperMapper;
import com.ruoyi.course.mapper.SysPaperTopicMapper;
import com.ruoyi.course.service.ISysCourseTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.SysCoursePaperMapper;
import com.ruoyi.course.domain.SysCoursePaper;
import com.ruoyi.course.service.ISysCoursePaperService;

/**
 * 课程试卷Service业务层处理
 * 
 * @author YJF
 */
@Service
public class SysCoursePaperServiceImpl implements ISysCoursePaperService 
{
    @Autowired
    private SysCoursePaperMapper sysCoursePaperMapper;

    @Autowired
    private SysPaperMapper sysPaperMapper;

    @Autowired
    private SysPaperTopicMapper sysPaperTopicMapper;

    @Autowired
    private ISysCourseTopicService sysCourseTopicService;

    /**
     * 查询课程试卷
     * 
     * @param sysCoursePaper 课程试卷
     * @return 课程试卷
     */
    @Override
    public SysCoursePaper selectSysCoursePaperByCoursePaperId(SysCoursePaper sysCoursePaper)
    {
        return sysCoursePaperMapper.selectSysCoursePaperByCoursePaperId(sysCoursePaper);
    }

    /**
     * 查询课程试卷列表
     * 
     * @param sysCoursePaper 课程试卷
     * @return 课程试卷
     */
    @Override
    public List<SysCoursePaper> selectSysCoursePaperList(SysCoursePaper sysCoursePaper)
    {
        return sysCoursePaperMapper.selectSysCoursePaperList(sysCoursePaper);
    }

    /**
     * 新增课程试卷
     * 
     * @param sysCoursePaper 课程试卷
     * @return 结果
     */
    @Override
    public int insertSysCoursePaper(SysCoursePaper sysCoursePaper)
    {
        //同一课程里试卷不能重复
        SysCoursePaper coursePaper = new SysCoursePaper();
        coursePaper.setDelFlag(SysConstants.CONSTANT_STR0);
        coursePaper.setCourseNo(sysCoursePaper.getCourseNo());
        coursePaper.setPaperId(sysCoursePaper.getPaperId());
        SysCoursePaper paperInfo = sysCoursePaperMapper.selectSysCoursePaperByCoursePaperId(coursePaper);
        if (StringUtils.isNotNull(paperInfo)) {
            throw new ServiceException("请重新选择，课程：" + paperInfo.getCourseName() + " 中已经存在试卷：" + paperInfo.getPaperName());
        }
        //获取试卷信息
        SysPaper sysPaper = new SysPaper();
        sysPaper.setPaperId(sysCoursePaper.getPaperId());
        sysPaper.setPaperStatus(SysConstants.CONSTANT_INT0);
        sysPaper.setCheckStatus(SysConstants.CONSTANT_INT2);
        SysPaper paper = sysPaperMapper.selectSysPaperByPaperId(sysPaper);

        if(StringUtils.isNull(paper)){
            throw new ServiceException("获取试卷信息异常,请确认试卷是否审核通过并状态正常");
        }
        //获取题目总数
        SysPaperTopic paperTopic = new SysPaperTopic();
        paperTopic.setPaperId(sysCoursePaper.getPaperId());
        List<SysPaperTopic> topicList = sysPaperTopicMapper.selectSysPaperTopicList(paperTopic);
        if(StringUtils.isNull(topicList)){
            throw new ServiceException("获取题目信息异常,请确认试卷是否存在题目");
        }

        //组装课程试卷数据
        sysCoursePaper.setPaperName(paper.getPaperName());
        if(StringUtils.isNull(sysCoursePaper.getPaperType())){
            sysCoursePaper.setPaperType(paper.getPaperType());
        }
        if(StringUtils.isNull(sysCoursePaper.getPaperStatus())){
            sysCoursePaper.setPaperStatus(paper.getPaperStatus());
        }
        //试题总数
        sysCoursePaper.setTopicNum(paper.getTopicNum());
        sysCoursePaper.setPaperRemark(paper.getPaperRemark());
        sysCoursePaper.setUserId(SecurityUtils.getUserId());
        sysCoursePaper.setCreateTime(DateUtils.getNowDate());
        int result = sysCoursePaperMapper.insertSysCoursePaper(sysCoursePaper);

        //组装题目，批量插入数据
        List<SysCourseTopic> courseTopicList = new ArrayList<>();
        for(SysPaperTopic topic : topicList){
            SysCourseTopic courseTopic = new SysCourseTopic();
            courseTopic.setCoursePaperId(sysCoursePaper.getCoursePaperId());    //课程试卷ID
            courseTopic.setCourseNo(sysCoursePaper.getCourseNo());     //课程编号
            courseTopic.setTopicName(topic.getTopicName());      //题目名称
            courseTopic.setTopicIntro(topic.getTopicIntro());    //题目说明
            courseTopic.setTopicSort(topic.getSort());           //题目排序
            courseTopic.setTopicType(topic.getTopicType());     //题目类型
            courseTopic.setTopicHard(topic.getTopicHard());      //题目难度
            courseTopic.setTopicAnswer(topic.getTopicAnswer());     //题目难度
            courseTopic.setTopicStatus(topic.getTopicStatus());     //题目状态
            courseTopic.setTopicRemark(topic.getTopicRemark());     //题目备注
            courseTopic.setCreateTime(DateUtils.getNowDate());
            courseTopic.setUpdateTime(DateUtils.getNowDate());
            courseTopicList.add(courseTopic);
        }
        sysCourseTopicService.insertSysCourseTopicAll(courseTopicList);
        return result;
    }

    /**
     * 修改课程试卷
     * 
     * @param sysCoursePaper 课程试卷
     * @return 结果
     */
    @Override
    public int updateSysCoursePaper(SysCoursePaper sysCoursePaper)
    {
        //获取试卷信息
        SysPaper sysPaper = new SysPaper();
        sysPaper.setPaperId(sysCoursePaper.getPaperId());
        sysPaper.setPaperStatus(SysConstants.CONSTANT_INT0);
        sysPaper.setCheckStatus(SysConstants.CONSTANT_INT2);
        SysPaper paper = sysPaperMapper.selectSysPaperByPaperId(sysPaper);

        if(StringUtils.isNull(paper)){
            throw new ServiceException("获取试卷信息异常,请确认试卷是否审核通过并状态正常");
        }
        //组装课程试卷数据
        sysCoursePaper.setPaperName(paper.getPaperName());
        if(StringUtils.isNull(sysCoursePaper.getPaperType())){
            sysCoursePaper.setPaperType(paper.getPaperType());
        }
        if(StringUtils.isNull(sysCoursePaper.getPaperStatus())){
            sysCoursePaper.setPaperStatus(paper.getPaperStatus());
        }
        //试题总数
        sysCoursePaper.setTopicNum(paper.getTopicNum());
        sysCoursePaper.setPaperRemark(paper.getPaperRemark());
        sysCoursePaper.setUpdateTime(DateUtils.getNowDate());
        return sysCoursePaperMapper.updateSysCoursePaper(sysCoursePaper);
    }

    /**
     * 批量删除课程试卷
     * 
     * @param coursePaperIds 需要删除的课程试卷主键
     * @return 结果
     */
    @Override
    public int deleteSysCoursePaperByCoursePaperIds(Long[] coursePaperIds)
    {
        return sysCoursePaperMapper.deleteSysCoursePaperByCoursePaperIds(coursePaperIds);
    }

    /**
     * 删除课程试卷信息
     * 
     * @param coursePaperId 课程试卷主键
     * @return 结果
     */
    @Override
    public int deleteSysCoursePaperByCoursePaperId(Long coursePaperId)
    {
        return sysCoursePaperMapper.deleteSysCoursePaperByCoursePaperId(coursePaperId);
    }
}
